package com.wql.img;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;

/**
 * 图片缩放工具类
 *
 * @author wuqinglong
 * @time 下午3:42:42
 * @email hellowql@126.com
 */
public class ImgUtils {

	/**
	 * 按比例缩放
	 * 
	 * @param ratio
	 * @param src
	 * @param dest
	 */
	public static void zoom(float ratio, String src, String dest) {
		if (ratio < 0 || null == src || src.length() == 0 || null == dest || dest.length() == 0)
			return;
		BufferedImage source = getBufferedImageFromPath(src);
		int sWidth = source.getWidth(), sHeight = source.getHeight();
		zoom(Math.round(sWidth * ratio), Math.round(sHeight * ratio), src, dest);
	}

	/**
	 * 给定宽高的按比例缩放
	 * 
	 * @param width
	 * @param height
	 * @param src
	 * @param dest
	 */
	public static void zoom(int width, int height, String src, String dest) {
		if (width < 0 || height < 0 || null == src || src.length() == 0 || null == dest || dest.length() == 0)
			return;
		BufferedImage source = getBufferedImageFromPath(src);
		int sWidth = source.getWidth(), sHeight = source.getHeight();
		float ratio = Math.min((float) width / sWidth, (float) height / sHeight);
		write(source, Math.round(ratio * sWidth), Math.round(ratio * sHeight), dest);

	}

	/**
	 * 按给定宽高缩放
	 * 
	 * @param width
	 * @param height
	 * @param src
	 * @param dest
	 */
	public static void resize(int width, int height, String src, String dest) {
		if (width < 0 || height < 0 || null == src || src.length() == 0 || null == dest || dest.length() == 0)
			return;
		write(getBufferedImageFromPath(src), width, height, dest);
	}

	/**
	 * 通过路径获取图片缓存
	 * 
	 * @param src
	 * @return
	 */
	private static BufferedImage getBufferedImageFromPath(String src) {
		BufferedImage source = null;
		try {
			source = ImageIO.read(new File(src));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return source;
	}

	/**
	 * 按照给定的参数绘制图片
	 * 
	 * @param source
	 * @param width
	 * @param height
	 * @param dest
	 */
	private static void write(BufferedImage source, int width, int height, String dest) {
		BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2d = bufferedImage.createGraphics();
		bufferedImage = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
		g2d.dispose();
		g2d = bufferedImage.createGraphics();
		Image from = source.getScaledInstance(width, height, BufferedImage.SCALE_AREA_AVERAGING);
		g2d.drawImage(from, 0, 0, null);
		g2d.dispose();
		try {
			ImageIO.write(bufferedImage, "png", new File(dest));
		} catch (IOException e) {
			e.printStackTrace();
		}
		// BufferedImage bfimg = new BufferedImage(width, height,
		// source.getType());
		// Graphics graphics = bfimg.getGraphics();
		// Graphics2D graphics2d = (Graphics2D) graphics;
		// graphics2d.drawImage(source, 0, 0, width, height, null);
		// graphics2d.dispose();
		// try {
		// ImageIO.write(bfimg, dest.substring(dest.lastIndexOf(".") + 1), new
		// File(dest));
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
	}

	private static void test() {
		IMOperation op = new IMOperation();
		op.addImage("f:/222.jpg");
		op.crop(500, 500, 500, 500);
		op.resize(1000, 1000);
		op.addImage("f:/666.jpg");
		ConvertCmd convert = new ConvertCmd();
		// linux下不要设置此值，不然会报错
		convert.setSearchPath("C:/Program Files (x86)/ImageMagick-6.9.0-Q16");
		try {
			convert.run(op);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (IM4JavaException e) {
			e.printStackTrace();
		}
	}

}
